using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using UpdaterManagement.Data;
using UpdaterManagement.Models;
using Microsoft.AspNetCore.Authorization;

namespace UpdaterManagement.Controllers
{
    [Authorize]
    public class EnterpriseController : Controller
    {
        private readonly ApplicationDbContext _context;

        public EnterpriseController(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<IActionResult> Index(int page = 1, int pageSize = 5)
        {
            var query = _context.Enterprises.Where(e => e.IsActive);
            
            var totalCount = await query.CountAsync();
            var enterprises = await query
                .OrderByDescending(e => e.CreatedTime)
                .Skip((page - 1) * pageSize)
                .Take(pageSize)
                .ToListAsync();
            
            ViewBag.CurrentPage = page;
            ViewBag.PageSize = pageSize;
            ViewBag.TotalCount = totalCount;
            ViewBag.TotalPages = (int)Math.Ceiling((double)totalCount / pageSize);
            
            return View(enterprises);
        }

        public IActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public async Task<IActionResult> Create(Enterprise enterprise)
        {
            if (ModelState.IsValid)
            {
                var existingCode = await _context.Enterprises.FirstOrDefaultAsync(e => e.Code == enterprise.Code);
                if (existingCode != null)
                {
                    ViewBag.Error = "企业代码已存在";
                    return View(enterprise);
                }

                _context.Enterprises.Add(enterprise);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(enterprise);
        }

        public async Task<IActionResult> Edit(int id)
        {
            var enterprise = await _context.Enterprises.FindAsync(id);
            if (enterprise == null) return NotFound();
            return View(enterprise);
        }

        [HttpPost]
        public async Task<IActionResult> Edit(Enterprise enterprise)
        {
            if (ModelState.IsValid)
            {
                var existingCode = await _context.Enterprises.FirstOrDefaultAsync(e => e.Code == enterprise.Code && e.Id != enterprise.Id);
                if (existingCode != null)
                {
                    ViewBag.Error = "企业代码已存在";
                    return View(enterprise);
                }

                _context.Enterprises.Update(enterprise);
                await _context.SaveChangesAsync();
                return RedirectToAction(nameof(Index));
            }
            return View(enterprise);
        }

        [HttpPost]
        public async Task<IActionResult> Delete(int id)
        {
            var enterprise = await _context.Enterprises.FindAsync(id);
            if (enterprise != null)
            {
                enterprise.IsActive = false;
                await _context.SaveChangesAsync();
            }
            return RedirectToAction(nameof(Index));
        }
    }
}